package com.iwop.moto.controller.web;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.MGPOrder;
import com.iwop.moto.model.PO.PayMentPO;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.web.PayMentService;
import com.iwop.moto.utils.stringUtil.StringUtils;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * #author chenzq
 * #create 2017-01-18-13:40
 */
@Controller
@RequestMapping("platform")
public class PayMentController extends BaseController {


    @Autowired
    private PayMentService payMentService;

    @RequestMapping(value = "/payMent/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel orderPurchaseList(@ApiParam(name = "rows", value = "行数", defaultValue = "")
                                         @RequestParam(value = "rows") int rows,
                                         @ApiParam(name = "pages", value = "页码", defaultValue = "")
                                         @RequestParam(value = "pages") int pages,
                                         @ApiParam(name = "startTime", value = "交易时间", required = false, defaultValue = "")
                                         @RequestParam(value = "startTime", required = false) String startTime,
                                         @ApiParam(name = "endTime", value = "交易结束时间", required = false, defaultValue = "")
                                         @RequestParam(value = "endTime", required = false) String endTime,
                                         @ApiParam(name = "buyerName", value = "付款方姓名", required = false, defaultValue = "")
                                         @RequestParam(value = "buyerName", required = false) String buyerName,
                                         @ApiParam(name = "buyerTel", value = "付款方手机号", required = false, defaultValue = "")
                                         @RequestParam(value = "buyerTel", required = false) String buyerTel,
                                         @ApiParam(name = "sellerName", value = "收款方姓名", required = false, defaultValue = "")
                                         @RequestParam(value = "sellerName", required = false) String sellerName,
                                         @ApiParam(name = "sellerTel", value = "收款方手机号", required = false, defaultValue = "")
                                         @RequestParam(value = "sellerTel", required = false) String sellerTel,
                                         @ApiParam(name = "orderType", value = "订单类型", required = false, defaultValue = "")
                                         @RequestParam(value = "orderType", required = false) String orderType,
                                         @ApiParam(name = "outTradeNo", value = "订单号", required = false, defaultValue = "")
                                         @RequestParam(value = "outTradeNo", required = false) String outTradeNo,
                                         @ApiParam(name = "payTradeNo", value = "交易号", required = false, defaultValue = "")
                                         @RequestParam(value = "payTradeNo", required = false) String payTradeNo,
                                         @ApiParam(name = "payType", value = "订单类型", required = false, defaultValue = "")
                                         @RequestParam(value = "payType", required = false) String payType,
                                         @ApiParam(name = "mchType", value = "支付方式", required = false, defaultValue = "")
                                         @RequestParam(value = "mchType", required = false) String mchType,
                                         @ApiParam(name = "export", value = "是否导出", required = false, defaultValue = "")
                                         @RequestParam(value = "export", required = false) String export,
//                                         @ApiParam(name = "flag", value = "传yes匹配正确的收入，no匹配错误的收入（与支付宝微信收款对比）", required = false, defaultValue = "")
//                                             @RequestParam(value = "flag", required = false) String flag,
                                         HttpServletResponse response
    ) {
        try {
            if (pages < 1) {
                pages = 1;
            }
            if (rows < 1) {
                rows = 1;
            }
            int offset = (pages - 1) * rows;

            SuperRecordModel superRecordModel = new SuperRecordModel();
            superRecordModel.put("row", rows);
            superRecordModel.put("offset", offset);
            superRecordModel.put("startTime", startTime);
            superRecordModel.put("endTime", endTime);
            superRecordModel.put("buyerName", buyerName);
            superRecordModel.put("buyerTel", buyerTel);
            superRecordModel.put("sellerName", sellerName);
            superRecordModel.put("mchType", mchType);
            superRecordModel.put("sellerTel", sellerTel);
            superRecordModel.put("orderType", orderType);
            superRecordModel.put("outTradeNo", outTradeNo);
            superRecordModel.put("payTradeNo", payTradeNo);
            superRecordModel.put("payType", payType);
            if (StringUtils.isEmpty(export)) {
                SuperRecordModel srm = new SuperRecordModel();
                List<PayMentPO> payMentList = payMentService.findPayMentList(superRecordModel);
                PayMentPO sumMap = payMentService.sumPayMoney(superRecordModel);
                Long aLong = payMentService.findCount(superRecordModel);
                Double sumCounponMoney = payMentService.sumCounponMoney();
                srm.put("payMentList", payMentList);
                srm.put("sumMap", sumMap);
                return getSucceed(srm, ErrCode.SUCCEED, aLong + "");
            } else {
                List<PayMentPO> payMentPO = payMentService.findPayMentAll(superRecordModel);
                String title[] = {"交易时间", "付款方姓名", "付款方手机号", "收款方姓名", "收款方手机号", "订单类型", "订单号", "支付方式", "交易号", "支付类型", "订单金额", "付款金额", "优惠券", "推广金", "服务费"};
                List<List> lists = new ArrayList<List>();
                for (PayMentPO po : payMentPO) {
                    List<String> cusList = new ArrayList<>();
                    cusList.add(po.getBuyerTime());
                    cusList.add(po.getBuyerName());
                    cusList.add(po.getBuyerTel());
                    cusList.add(po.getSellerName());
                    cusList.add(po.getSellerTel());
                    cusList.add(po.getOrderType());
                    cusList.add(po.getOutTradeNo());
                    Object mchType1 = po.getMchType();
                    String mchtype = "未知类型";
                    if (mchType1 == null) {
                        mchtype = "未知类型";
                    } else if (mchType1.equals("UNIONPAY")) {
                        mchtype = "银联支付";
                    } else if (mchType1.equals("WXPAY")) {
                        mchtype = "微信支付";
                    } else if (mchType1.equals("ALIPAY")) {
                        mchtype = "支付宝支付";
                    } else if (mchType1.equals("BALANCE")) {
                        mchtype = "余额支付";
                    }
                    cusList.add(mchtype);
                    cusList.add(po.getPayTradeNo());
                    cusList.add(po.getPayType());

                    Double payMoney =Double.parseDouble(po.getPayMoney());
                    Double payFee = 0d;
                    if (po.getPayFee() != null) {
                        payFee =Double.parseDouble(po.getPayFee());
                    }
                    cusList.add(String.valueOf(BigDecimal.valueOf(payFee+payMoney)));
                    cusList.add(po.getPayActualMoney());
                    cusList.add(po.getCouponMoney() + "");
                    cusList.add(po.getBrokerage() + "");
                    cusList.add(po.getPayFee());
                    lists.add(cusList);
                }
                response.reset();
                response.setContentType("octets/stream");
                String fileName = "订单交易记录表.xls";
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
                OutputStream outputStream = response.getOutputStream();
                StringUtils.writeExcel(outputStream, title, lists, "交易记录");
                return getSucceed(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
